﻿namespace Huayou.Common
{
    using System;

    public class Tree : BinaryTree
    {
        protected BinaryTreeNode cursorNode = null;

        public void AddBrother(object val)
        {
            if (base.root == null)
            {
                base.Insert(val);
                this.MoveNode(base.root);
            }
            else
            {
                if (this.cursorNode == null)
                {
                    this.MoveNode(base.root);
                }
                this.AddBrother(this.cursorNode, val);
            }
        }

        public void AddBrother(BinaryTreeNode node, object val)
        {
            this.MoveNode(node);
            Console.WriteLine(string.Concat(new object[] { "addbrother ", val, " on ", this.cursorNode.GetValue() }));
            while (base.HasRight())
            {
                base.MoveRight();
            }
            base.MoveRight();
            base.Insert(val);
        }

        public void AddChild(object val)
        {
            if (base.root == null)
            {
                base.Insert(val);
                this.MoveNode(base.root);
            }
            else
            {
                if (this.cursorNode == null)
                {
                    this.MoveNode(base.root);
                }
                this.AddChild(this.cursorNode, val);
            }
        }

        public void AddChild(BinaryTreeNode node, object val)
        {
            this.MoveNode(node);
            if (base.HasLeft())
            {
                base.MoveLeft();
                while (base.HasRight())
                {
                    base.MoveRight();
                }
                base.MoveRight();
                base.Insert(val);
            }
            else
            {
                base.MoveLeft();
                base.Insert(val);
            }
        }

        public void MoveNode(BinaryTreeNode node)
        {
            this.cursorNode = node;
            base.MoveNode(node);
        }

        public void MoveRoot()
        {
            this.MoveNode(base.root);
        }
    }
}

